﻿using System.Linq;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using QRCodePass.Core;
using QRCodePass.Core.Models;
using QRCodePass.EntityFrameworkCore.EntityMapper;
using QRCodePass.EntityFrameworkCore.Extensions;

namespace QRCodePass.EntityFrameworkCore
{
    public class AppDbContext : IdentityDbContext<ApplicationUser>
    {
        //注意:将ApplicationUser作为泛型参数传递给IdentityDbContext类

        public AppDbContext(DbContextOptions<AppDbContext> options)
          : base(options)
        {
        }

        public DbSet<Student> Students { get; set; }
        public DbSet<Process> Processes { get; set; }
        public DbSet<StudentProcess> StudentProcess { get; set; }
        public DbSet<DepartProc> DepartProc { get; set; }
        public DbSet<Department> Departments { get; set; }

        public DbSet<Build> Build { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Seed();

            //获取当前系统中所有领域模型上的外键列表
            var foreignKeys = modelBuilder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys());
            foreach (var foreignKey in foreignKeys)
            {
                //然后将它们的删除行为配置为 Restrict即无操作
                foreignKey.DeleteBehavior = DeleteBehavior.Restrict;
            }
            //modelBuilder.ApplyConfiguration(new BlogMapper());
            //modelBuilder.ApplyConfiguration(new PostMapper());
            modelBuilder.ApplyConfiguration(new DepartmentProcessMapper());
            modelBuilder.ApplyConfiguration(new StudentProcessMapper()); 
        }
    }
}